<?php
/**
 * Created by .
 * User: luogan
 * Date: 2021/12/22
 */

namespace App\Service;


use App\Exception\ErrorException;
use App\Model\Admin;

class AdminService
{

    public function list($data)
    {
        $result = Admin::query()->paginate(10);
        return $result;
    }

    public function create($data)
    {
        $data['salt'] = 9527;
        $data['password'] =  md5($data['salt'] . $data['password']);
        $result = Admin::create($data);
        if (!$result){
            throw new ErrorException();
        }
        admin_log('新增管理员');
        return $result;
    }

    public function update($data)
    {
        if (isset($data['password'])){
            $data['salt'] = 9527;
            $data['password'] =  md5($data['salt'] . $data['password']);
        }
        $result = Admin::query()->where('id',$data['id'])->update($data);
        if (!$result){
            throw new ErrorException();
        }
        admin_log('更新管理员');
        return $result;
    }

    public function detail($id)
    {
        $result = Admin::query()->find($id);
        if (!$result){
            throw new ErrorException();
        }
        return $result;
    }

    public function delete($id)
    {
        $result = Admin::destroy($id);
        if (!$result){
            throw new ErrorException();
        }
        return $result;
        admin_log('删除管理员');
    }
}
